'use strict';

var userModel = require('../../config/models').userModel,
  roleModel = require('../../config/models').roleModel,
  menuModel = require('../../config/models').menuModel,
  operatorModel = require('../../config/models').operatorModel,
  relationModel = require('../../config/models').relationModel;

/*roleModel.find({}, function(err, role) {
  if (err) {
    console.log(err);
  } else {
    // console.log(role);
    // console.log(role[0].purview[0].operatorList);
    menuModel.find({}, function(err, menu) {
      if (err) {
        console.log(err);
      } else {
        operatorModel.find({}, function(err, operator) {
          if (err) {
            console.log(err);
          } else {
            role[0].update({$set: {
              purview: [{
                menu: menu[0],
                operatorList:[operator[0], operator[1]]
              }, {
                menu: menu[1],
                operatorList:[operator[0], operator[2]]
              }]
            }}, function(err, res) {
              if (err) {
                console.log(err);
              } else {
                console.log(res);
              }
            });
            role[1].update({$set: {
              purview: [{
                menu: menu[1],
                operatorList:[operator[0], operator[1], operator[2]]
              }, {
                menu: menu[2],
                operatorList:[operator[1], operator[2]]
              }]
            }}, function(err, res) {
              if (err) {
                console.log(err);
              } else {
                console.log(res);
              }
            });
          }
        });
      }
    });
  }
});*/
userModel.find({}, function(err, user) {
  if (err) {
    console.log(err);
  } else {
    // console.log(user);
    relationModel.create([{userId: user[0]._id, name: 'myFriends11', friends: [user[1]._id]},
      {userId: user[1]._id, name: 'myFriends11', friends: [user[0]._id]}], function(err, res) {
        if (err) {
          console.log(err);
        } else {
          console.log(res);
        }
    });
    /*relationModel.findOne({userId: user[0]}, function(err, relation) {
      if (err) {
        console.log(err);
      } else {
        console.log(relation);
        relation.update({}, {$push: {friends: user[1]}}, function(err, res) {
          if (err) {
            console.log(err);
          } else {
            console.log(res);
          }
        });
      }

    });
    relationModel.findOne({userId: user[1]}, function(err, relation) {
      if (err) {
        console.log(err);
      } else {
        console.log(relation);
        relation.update({}, {$push: {friends: user[0]}}, function(err, res) {
          if (err) {
            console.log(err);
          } else {
            console.log(res);
          }
        });
      }

    });*/
    /*relationModel.findOneAndUpdate({userId: user[0]}, {$addToSet: {friends: user[1]}}, function(err, res) {
      if (err) {
        console.log(err);
      } else {
        console.log(res.friends);
      }
    });
    relationModel.findOneAndUpdate({userId: user[1]}, {$addToSet: {friends: user[0]}}, function(err, res) {
      if (err) {
        console.log(err);
      } else {
        console.log(res.friends);
      }
    });*/
    /*user[0].update({'relationship.name': 'myFriends11'}, {$addToSet: {'relationship.$.userIdList': user[1]}}, function(err, res) {
      if (err) {
        console.log(err);
      } else {
        console.log(res);
      }
    });*/
    /*roleModel.find({}, function(err, role) {
      if (err) {
        console.log(err);
      } else {
        // console.log(role);
        user[0].update({$set: {roleId: role[0]}}, function(err, res) {
          if (err) {
            console.log(err);
          } else {
            console.log(res);
          }
        });
        user[1].update({$set: {roleId: role[1]}}, function(err, res) {
          if (err) {
            console.log(err);
          } else {
            console.log(res);
          }
        });
      }
    });*/
  }
});
